#include<bits/stdc++.h> 
using namespace std;

struct Cell {
    int i, j, d;
    Cell(int i, int j, int d) : i(i), j(j), d(d) {}
};

int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        int n;
        scanf("%d", &n);
        vector<vector<int>> g(n, vector<int>(n));
        int c = (n - 1) / 2; 
        
        vector<Cell> cells;
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                int d = abs(i - c) + abs(j - c);
                cells.emplace_back(i, j, d);
            }
        }
        
        sort(cells.begin(), cells.end(), [](const Cell& a, const Cell& b) {
            if (a.d != b.d) return a.d < b.d;
            if (a.i != b.i) return a.i < b.i;
            return a.j < b.j;
        });
        
        int num = 0;
        for (auto& cell : cells) {
            g[cell.i][cell.j] = num++;
        }
        
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                printf("%d ", g[i][j]);
            }
            printf("\n");
        }
    }
    return 0;
}
